import {
    getCategoryList
} from '../../../api/goods';
import {
    spuPage
} from '../../../api/goodsdetail';
Page({
    data: {
        oneHeight: 0,
        oneTop: 0,
        headerHeight: 0,
        categoryLeftList: [],
        indexId: '',
        pageIndex: 1,
        goodsList: [],
        total: 0,
        pageLoading: false,
        keyword: ''

    },
    onLoad() {
        let menuRect = wx.getMenuButtonBoundingClientRect();
        let deviceWidth = wx.getSystemInfoSync().windowWidth;
        let rpx = (750 / deviceWidth) * Number(menuRect.height + menuRect.top);
        let headerHeight = Math.floor(rpx);
        this.setData({
            oneHeight: menuRect.height,
            oneTop: menuRect.top,
            headerHeight: headerHeight + 126
        })
    },
    onShow() {
        let categoryId = wx.getStorageSync('categoryId')
        if (categoryId && this.data.indexId == categoryId) {
            return false
        }
        this.setData({
            pageIndex: 1,
            goodsList: [],
            indexId: categoryId
        });
        this.getCategoryLists();
    },
    async getCategoryLists() {
        const res = await getCategoryList();
        if (res.length > 0) {
            let id = res[0].id;
            if (this.data.indexId) {
                res.map(item => {
                    if (item.id == this.data.indexId) {
                        id = item.id
                    }
                })
            }
            this.setData({
                categoryLeftList: res,
                indexId: id
            });
            if (this.data.indexId) {
                this.setData({
                    indexId: this.data.indexId || res[0].id
                });
            }
            this.getSpuPage();
        }
    },
    async leftTbableNav(e) {
        this.setData({
            indexId: e.currentTarget.dataset.id
        });
        wx.setStorageSync('categoryId', e.currentTarget.dataset.id)
        this.setData({
            pageIndex: 1,
            goodsList: []
        });
        this.getSpuPage();
    },
    async getSpuPage() {
        this.setData({
            pageLoading: true
        })
        const {
            keyword,
            pageIndex,
            indexId

        } = this.data;
        let query = '?keyword=' + keyword + '&pageNo=' + pageIndex + '&pageSize=10' + '&categoryId=' + indexId
        const res = await spuPage(query);
        setTimeout(() => {
            let list = res.list
            list.map(item => {
                item.price = (item.price / 100).toFixed(2);
            })
            let goodsList = this.data.pageIndex == 1 ? list : [...this.data.goodsList, ...list]
            this.setData({
                goodsList: goodsList,
                total: res.total,
                pageLoading: false
            });
        }, 200)
    },
    loadGoodsList() {
        if (this.data.pageIndex * 10 < this.data.total && !this.data.pageLoading) {
            this.setData({
                pageIndex: this.data.pageIndex+1
            });
            this.getSpuPage();
        }
    },
    moreShop() {
        wx.navigateTo({
            url: '/pages/goods/nSearch/index',
        })
    },
    jumpToDetail(event) {
        let goodsId = event.currentTarget.dataset.id
        wx.navigateTo({
            url: '/pages/goods/details/index?goodsId=' + goodsId
        })
    },
    enterGoods(e) {
        let val = e.detail.value
        this.setData({
            keyword: val,
        });
        this.setData({
            pageIndex: 1,
            goodsList: []
        });
        this.getSpuPage();
    },
    blurGoods(e) {
        let val = e.detail.value
        this.setData({
            keyword: val,
        });
    },
    closeInput() {
        this.setData({
            keyword: '',
        });
    }
});